//分享裂变
const sharedetail = require('./service/sharedetail');
const sharerule = require('./service/sharerule');
const Joi = require('joi');
const schemaJoi = require('./utility/schemaJoi');
const indexService = require('./service/indexService');
exports.main = async (context) => {
   console.log('index.main')
    return await sharedetail.test(context.data);
};
//+++++++++++++++++++++++++++++分享记录表+++++++++++++++++++++++++++++++++++++++++
/**
 * 插入分享记录
 */
exports.insertSharerecord=async function(context) {
  let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			mixnick: Joi.string().required(),//混淆昵称
			nickname: Joi.string().required(),//用户昵称
      shared_mixnick: Joi.string().required(),//被分享人混淆昵称
			shared_nickname: Joi.string().required(),//被分享人昵称
      shared_img: Joi.string().required(),//被分享人头像
		});
		await schemaJoi(schema, ctx.data);

		//业务处理

		const retObj = await sharedetail.insertOne(ctx.data);
		return retObj;
	} catch (e) {
		console.log('error--------------e:::', e);
    return { success:false, msg: "请求失败", errInfo: e };
	}
}

/**
 * 获取分享列表
 */
exports.shareList = async function(context){
   let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			mixnick: Joi.string().required(),//混淆昵称
		});
		await schemaJoi(schema, ctx.data);

		//业务处理

		const retObj = await sharedetail.shareList(ctx.data);
		return retObj;
	} catch (e) {
		console.log('error--------------e:::', e);
    return { success:false, msg: "请求失败", errInfo: e };
	}
}


/**
 * 分享成功的用户列表
 */
exports.shareSuccessList = async function(context){
   let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			mixnick: Joi.string().required(),//混淆昵称
		});
		await schemaJoi(schema, ctx.data);

		//业务处理

		const retObj = await sharedetail.shareSuccessList(ctx.data);
		return retObj;
	} catch (e) {
		console.log('error--------------e:::', e);
    return { success:false, msg: "请求失败", errInfo: e };
	}
}


/**
 * 更新被分享人的会员状态
 */
exports.updateShareVip = async function(context){
   let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			mixnick: Joi.string().required(),//用户昵称
      shared_mixnick: Joi.string().required(),//被分享人混淆昵称
			shared_nickname:Joi.string().required(),//被分享人昵称
      rule_id: Joi.number().required(),//规则id
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
		//查询会员状态信息
		let isVip = await getMember(ctx);
		console.log(isVip.data,'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv')
		ctx.data.isVip=isVip.data;
		const retObj = await sharedetail.updateShareVip(ctx.data);
		return retObj;
	} catch (e) {
		console.log('error--------------e:::', e);
    return { success:false, msg: "请求失败", errInfo: e };
	}
}


/**
 * 查询用户会员信息
 * @param {nickName: string} 明文nick
 * @param {session: string} 
 */
async function getMember  (ctx) {
  try {
    const { data: { shared_nickname = "", session = "" }, cloud } = ctx
    console.log('获取会员信息',shared_nickname)
    const result = await cloud.topApi.invoke({
      api: 'taobao.crm.member.identity.get',
     // autoSession:true,
      data: {
        'nick': shared_nickname,
        'session': '6101101a81f5be33237c2dd921471224fc0d52e3f1c83084260076097' //context.accessToken
      }
    });
    console.log('会员信息',result)
    return {
      success: true,
      data: result.result.member_info
    };
  } catch (err) {
    return {
      success: false,
      data: err
    }
  }
}



/**
 * 获取入会链接
 * @param {url: string} 回调链接
 * @param {session: string} 
 */
exports.getMemberUrl = async (context) => {
  let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			url: Joi.string().required(),//回调链接
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
		const retObj = await indexService.getMemberUrl(ctx);
		return retObj;
	} catch (e) {
		console.log('error--------------e:::', e);
    return { success:false, msg: "请求失败", errInfo: e };
	}
};


//+++++++++++++++++++++++分享规则表+++++++++++++++++++++++++++++++++
/**
 * 添加分享规则
 */
exports.insertRule = async function(context){
   let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
			rule_name: Joi.string().required(),//规则名称
      description: Joi.string().required(),//规则描述
      share_number: Joi.number().required(),//分享人数
      state: Joi.number().required(),//被分享人的会员状态，0，非会员,1,老会员，2，新会员
      start_date: Joi.string().required(),//开始时间
      end_date: Joi.string().required(),//结束时间
		});
		await schemaJoi(schema, ctx.data);

		//业务处理

		const retObj = await sharerule.insertOne(ctx.data);
		return retObj;
	} catch (e) {
		console.log('error--------------e:::', e);
    return { success:false, msg: "请求失败", errInfo: e };
	}
}

/**
 * 删除规则
 */
exports.deleteRule = async function(context){
   let ctx = context;
	try {
		//参数验证
		const schema = Joi.object().keys({
      id: Joi.number().required(),//规则id
		});
		await schemaJoi(schema, ctx.data);

		//业务处理
		console.log(ctx.data)
		const retObj = await sharerule.deleteById(ctx.data);
		return retObj;
	} catch (e) {
		console.log('error--------------e:::', e);
    return { success:false, msg: "请求失败", errInfo: e };
	}
}

// /**
//  * 更新规则
//  */
// exports.updateRule = async function(context){
//    let ctx = context;
// 	try {
// 		//参数验证
// 		const schema = Joi.object().keys({
//       id: Joi.number().required(),//规则id，
//       data:Joi.object.required(),//规则内容
// 		});
// 		await schemaJoi(schema, ctx.data);

// 		//业务处理

// 		const retObj = await sharerule.updateOne(ctx);
// 		return retObj;
// 	} catch (e) {
// 		console.log('error--------------e:::', e);
//     return { success:false, msg: "请求失败", errInfo: e };
// 	}
//}